<?php 
	if($this->enterprise){
		$address  		= $this->enterprise->contact_address;	
		$company_name   = $this->enterprise->company_name_vn;
	}
?>
<?php 
	$config = Tomato_Config::getConfig();
	$googleApiKey = $config->localization->googlemap->apikey;	
?>
<?php if($this->showMap): //show map chi kiem tra enterprise active, ?>
	<?php if($this->enterprise_id):?>
	<?php $host = $_SERVER['HTTP_HOST'];?>	
	
		<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/swfobject/swfobject.js"></script>	
		<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/i18n/swfobject.js"></script>
		<?php if($this->has_dialog):?>
			<link media="all" type="text/css" href="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/themes/base/ui.core.css" rel="stylesheet">				
			<link media="all" type="text/css" href="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/themes/base/ui.resizable.css" rel="stylesheet">		
			<link media="all" type="text/css" href="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/themes/base/ui.dialog.css" rel="stylesheet">
			<link media="all" type="text/css" href="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/themes/base/ui.theme.css" rel="stylesheet">
			
			<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/ui.core.js"></script>		
			<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/ui.draggable.js"></script>		
			<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/ui.resizable.js"></script>
			<script language="JavaScript" type="text/JavaScript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.ui/ui.dialog.js"></script>		

			<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=<?php echo $googleApiKey;?>" type="text/javascript"></script>				
				
			
			<script language="javascript">	
			var googleZoomDefault = 10;
			var googleZoom   = 10;
			var mapElementId = "googleMapContent";
			var starTechMap  = null;
			function buildEvent(startechMap){
				GEvent.addListener(startechMap._map, 'click', function() {				
				});					
			}
			function BuildMarker(startechMap){		
				startechMap._map.addOverlay(startechMap._marker);
				var htmlMarker = "<div class='starTechGoogleMapMarker'><?php echo $company_name;?></div>";		
				GEvent.addListener(startechMap._marker, "dragend", function() {
					//startechMap._marker.openInfoWindowHtml(htmlMarker);		
					//startechMap._map.setCenter(startechMap._marker.getLatLng());				
				  });		  
				GEvent.addListener(startechMap._marker, "click", function() {
					//startechMap._marker.openInfoWindowHtml(htmlMarker);
				  });
				GEvent.trigger(startechMap._marker, "click");				
			}		
			function initGoogleMap(mapElementId,latlng,zoom){
				if (GBrowserIsCompatible()) {
					var map = new GMap2(document.getElementById(mapElementId));
					map.setCenter(latlng, googleZoom);
					
					map.setUIToDefault();
					marker = new GMarker(latlng, {draggable: true});			
					var startechMap = {
						_map: map,
						_marker: marker
					};
					BuildMarker(startechMap);
					buildEvent(startechMap);
					return startechMap;
				  }	
			}		
			function googleMapCallBack(data){
				if(data!= null){
					var PlaceMark	   = null;				
					var LocalPoint     = null;				
					if(data.Placemark.length>0){
						Placemark 	   = data.Placemark[0];
						LocalPoint     = Placemark.Point.coordinates;
					}
					if(LocalPoint!=null){
						var latlng 	 = new google.maps.LatLng(LocalPoint[1],LocalPoint[0]);
						starTechMap  = initGoogleMap(mapElementId,latlng,googleZoom);
					}
				}else alert("No result");			
			}
			function getJsonGoogleMap(address){
				jQuery.ajax({  
								dataType: 'jsonp',  
								url: 'http://maps.google.com/maps/geo?output=json&oe=utf8&sensor=false'  
										+ '&key=' + "<?php echo $googleApiKey;?>" + '&q=' + address,  
								cache: false,  
								success: function(data){
									googleMapCallBack(data);
								 }
							});		
			}
			$(document).ready(function(){
				var l_x_viewer = 300;
				var l_y_viewer = 264;
				var mapViewerTop 		= $("#mapViewerTop");
				var mapViewerLeft 		= $("#mapViewerLeft");
				var mapViewerRight 		= $("#mapViewerRight");
				var mapViewerBottom 	= $("#mapViewerBottom");
				var pos_y 	= (500 - l_x_viewer) / 2; // ngang
				var pos_x   = (460 - l_y_viewer) / 2; // doc
				mapViewerTop.css("top",pos_y)
							.css("left",pos_x);
				mapViewerLeft.css("top",pos_y)
							.css("left",pos_x);
				mapViewerRight.css("top",pos_y)
							.css("left",pos_x   + l_x_viewer);
				mapViewerBottom.css("top",pos_y + l_y_viewer)
							.css("left",pos_x);
				$("#btnsearchPlaceStarTechMap").click(function(){
					googleZoom  = googleZoomDefault;
					var address = $("#txtAddressStarTechMap").val();
					getJsonGoogleMap(address);	
					return false;				
				});
				$("#btnSaveMapStarTechMap").click(function(){				
					var mapCenter 		= starTechMap._map.getCenter();
					var markerLatLng 	= starTechMap._marker.getLatLng();
					var zoom 			= starTechMap._map.getZoom();
					var width			= $("#"+mapElementId).width();
					var height			= $("#"+mapElementId).height();
					var markerSize      = 'mid'; // mid,tiny,small
					var colorMarker     = '0xFFFF00';
					var Clabel			= 'E';				
					var center = '&center='+mapCenter.y+','+mapCenter.x;
					var zoom   = '&zoom=' + zoom;
					var size   = '&size='+width+'x'+height;
					var markers ='&markers=size:' + markerSize + '%7Ccolor:' + colorMarker + '%7Clabel:' + Clabel+'%7C' + markerLatLng.y + ','+ markerLatLng.x;;	
					var sensor = '&sensor=true';					
					var href = "<?php echo $this->url(array(),'bizprofile_enterprise_savemap');?>";
					$.ajax({
						url: href,
						type: 'post',
						data: 'enterprise_id=<?php echo $this->enterprise_id?>' + center + size + zoom + markers +  sensor,
						dataType: 'html',
						success: function(data){ 
							alert("<?php echo $this->translator('edit_map_successfull','bizprofile');?>");
							if($("#enterpriseMapImage").size()>0){
								var myDate=new Date();
								var src= $("#enterpriseMapImage").attr('src');
								$("#enterpriseMapImage").attr('src',src+'?time='+myDate.toUTCString());
								$("#enterpriseMapImage").show();								
							}
						}
					});
					$(".containerGoogleMap").show();
					return false;
				});
				$("#editEnterpriseMap").click(function(){
					$("#googleMapDialog").dialog({
						title: '<?php echo $this->translator("edit_map",'bizprofile');?>',
						dialogClass: 't_a_ui_dialog',
						position: 'center',
						modal: true,
						closeOnEscape: false,
						resizable: false,
						/*resizeStop: function(event, ui) {
							var height = ui.size.height;
							var width  = ui.size.width;
							$("#googleMapDialog").height(height-20);
							$("#googleMapDialog").width(width);
							//var address = $("#txtAddressStarTechMap").val();
							//getJsonGoogleMap(address);						
						},*/
						width: 502,
						height: 600,					
						close: function(event, ui) {
							$(this).dialog('destroy');                       
						},
						open: function(event, ui){
							var address = $("#txtAddressStarTechMap").val();
							getJsonGoogleMap(address);						
						}
					});
					return false;
				});	
			});
			</script>		
			<style>
				#googleMapDialog{
					width: 500;
					height: 550px;
					display: none;
					text-align: left;
					padding: 0px;
				}
				#googleMapDialogFilter{
					height: 40px;
					margin: 0pt auto;
					border: none;
					padding: 0px;
					list-style: none;
					color: black;
				}
				#googleMapDialogFilter input[type=text]{
					border: 1px solid #CCCCCC;
				}
				#googleMapDialogFilter label{
					width: auto;
				}
				#googleMapDialogFilter button{
					background: #D55900;
					border: 1px solid  #CCCCCC;
					color: #FFFFFF;
					cursor: pointer;
					font-weight: normal;
					overflow: visible;
					padding: 3px;		
				}
				#txtAddressStarTechMap{
					width: 200px;
				}
				#googleMapDialogFilter li{
					float: left;
					display: relative;
				}
				.horizontalMapViewer{
					height: 2px;
					width: 300px;
					background-color: blue;
					position: absolute;
					z-index: 10000;
				}
				.verticalMapViewer{
					width: 2px;
					height: 264px;
					background-color:blue;
					position: absolute;
					z-index: 10000;
				}
				#googleMapContentContainer{
					width: 500px;
					height: 460px;
					margin: 0pt auto;
					padding: 0px;
					border: 1px solid #CCCCCC;	
					position: absolute;				
				}
				#googleMapContent{
					margin: 0pt auto;
					width: 500px;
					height: 460px;
				}
			</style>					
			<div id="googleMapDialog">	
					<ul id="googleMapDialogFilter">
						<li><label for="txtAddressStarTechMap"><?php echo $this->translator("address_map",'bizprofile');?></label></li>
						<li><input type="text" name="txtAddressStarTechMap" id="txtAddressStarTechMap" value="<?php echo $address;?>"/></li>
						<li style="float:right;margin-right:5px;"><button id="btnSaveMapStarTechMap"><?php echo $this->translator("btn_save_map",'bizprofile');?></button></li>
						<li style="float:right;"><button id="btnsearchPlaceStarTechMap"><?php echo $this->translator("btn_search_map",'bizprofile');?></button></li>
					</ul>
					<hr/>
					<div id="googleMapContentContainer">	
						<div id="mapViewerTop" class="horizontalMapViewer"></div>			
						<div id="mapViewerLeft" class="verticalMapViewer"></div>
						<div id="mapViewerRight" class="verticalMapViewer"></div>
						<div id="mapViewerBottom" class="horizontalMapViewer"></div>
						<div id="googleMapContent">
						</div>
					</div>
			</div>		
		<?php endif;?>	
		<div class="subcolumns map pb10">
			<?php if($this->has_dialog): ?>
				<a href="#" id="editEnterpriseMap" style="float:right;"><?php echo $this->translator("edit_map","bizprofile");?></a> 
			<?php endif;?>
			<?php if($this->has_flash):?>
				<div class="containerGoogleMap">
					<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="300" height="264" id="jacket_render" align="middle">
						<param name="allowScriptAccess" value="sameDomain" />
						<param name="allowFullScreen" value="false" />
						<param name="quality" value="high" />
						<param name="bgcolor" value="#ffffff" />
						<param name="movie" value="<?php echo $this->APP_STATIC_SERVER; ?>/skins/<?php echo $this->APP_TEMPLATE; ?>/<?php echo $this->APP_SKIN; ?>/swf/image_slide.swf?time=<?php echo time();?>" />
						<param name="flashVars" value="<?php echo $this->imageString;?>" />
						<embed src="<?php echo $this->APP_STATIC_SERVER; ?>/skins/<?php echo $this->APP_TEMPLATE; ?>/<?php echo $this->APP_SKIN; ?>/swf/image_slide.swf?time=<?php echo time();?>" flashVars="<?php echo $this->imageString;?>" quality="high" bgcolor="#ffffff" width="300" height="225" name="header" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
					</object>
				</div>			
			<?php endif; ?>
		</div>
	<?php endif;?>
<?php endif;?>
